package com.smsquiztest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsClient extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
        try{
            Bundle bundle = intent.getExtras();
            Object[] messages = (Object[])bundle.get("pdus");
            SmsMessage message = SmsMessage.createFromPdu((byte[]) messages[0]);
            String inputPhone = message.getDisplayOriginatingAddress();
            String listeningPhone = AppModel.getInstance().getPhoneNumber();
            if(messages.length > 0 && inputPhone.equals(listeningPhone)){
                AppLogger.Write("Message reseived");
                AppModel.getInstance().sendNextQuestion();
            }
        }
        catch (Exception ex){
            AppLogger.WriteError(ex);
        }
	}
}
